using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using Castle.MonoRail.Framework;
using Castle.MonoRail.ActiveRecordSupport;
using Castle.Components.Binder;

using Saffron.Framework.Users;
using Saffron.Filters;
using Saffron.Framework.Projects;


namespace Saffron.Controllers
{
    [Layout("default"), Rescue("generalerror")]
    [Filter(ExecuteEnum.BeforeAction, typeof(AuthenticationFilter))]
    public class TaskController:PublicBaseController
    {
        public void Index()
        {
            Project project = null;
            Task[] tasks = null;
            
            if (Query["ProjectID"] != null)
            {
                project = Project.FindByPrimaryKey(Int32.Parse(Query["ProjectID"]));
                tasks = Task.GetTasksByProject(project);

                PropertyBag["Project"] = project;
                PropertyBag["tasks"] = tasks;
             
            }
        }

     

        public void Create()
        {
            Project project = Project.FindByPrimaryKey(Int32.Parse(Query["ProjectID"]));
            PropertyBag["Project"] = project;
        }

        public void Edit()
        {
            Task task = Task.FindByPrimaryKey(Int32.Parse(Query["TaskId"]));
            PropertyBag["Project"] = task.TaskProject;
            PropertyBag["Task"] = task;
           
        }

        public void Update([ARDataBind("Task", AutoLoad = AutoLoadBehavior.Always, Validate = true)] Task task)
        {
            NameValueCollection param = new NameValueCollection();
           
            param.Add("projectId", task.TaskProject.Id.ToString());
            

            if (!HasValidationError(task))
            {
                task.Update();
                Redirect("Task", "Index", param);
            }
            else
            {
                Flash["Task"] = task;
                Flash["summary"] = GetErrorSummary(task);
                RedirectToAction("edit", param);
            }
        }
     

        public void CreateTask([ARDataBind("Task", Validate = true)] Task task )
        {
            Project project = Project.FindByPrimaryKey(Int32.Parse(Query["ProjectID"]));
            NameValueCollection param = new NameValueCollection();
            param.Add("projectId", project.Id.ToString());
            
          
            if (!HasValidationError(task))
            {
                task.CreatedDate = DateTime.Now;
                task.TaskProject = project;
                task.Create();
                Redirect("Task", "Index", param);
            }
            else
            {
                Flash["Task"] = task;
                Flash["summary"] = GetErrorSummary(task);
                RedirectToAction("create", param);
            }
        }

        public void Delete(int id)
        {
            Task task = Task.FindByPrimaryKey(id); 
            int projectId = task.TaskProject.Id;
            NameValueCollection param = new NameValueCollection();
            param.Add("projectId", projectId.ToString());
            task.Delete();
            RedirectToAction("index", param);
        }
       
    }
}
